package com.atwulidun.singleton01.enumeration5;

// 方式五：枚举
public class SingletonDemo {
    public static void main(String[] args) {
        // 证明两次获取到的是同一个对象
        Singleton instance1 = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance1 == instance2);

        // 用hashCode来证明两次获取到的是同一个对象
        System.out.println("instance1.hashCode = " + instance1.hashCode());
        System.out.println("instance2.hashCode = " + instance2.hashCode());

        instance1.sayHello();
    }
}

enum Singleton {
    // 成员
    INSTANCE;

    public void sayHello() {
        System.out.println("hello!");
    }
}
